#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

Global $field[4] = [122,310,622,460]

Global $test_points[15][2] = [ _
[18,30],[65,69],[130,40],[118,99],[186,133], _
[246,121],[293,79],[282,109],[320,76],[326,107], _
[399,96],[421,29],[464,8],[458,55],[488,6] _
]

Global $test_result[15][40][4]
For $i = 0 To 14
   $test_result[$i][0][0] = 0
Next
Global $file_in = "colors-in.txt"

Global $last_count_result[15][5]

Func dump_last_result($filename = False)
   Local $str = ""
   For $i = 0 To 14
	  $str &= "[" & $last_count_result[$i][0] & "," & $last_count_result[$i][1] & "]"
	  $str &= " 0x" & Hex($last_count_result[$i][2],6)
	  $str &= " " & $last_count_result[$i][3] & "-" & $last_count_result[$i][4] 
	  $str &= @CRLF
   Next
   
   If $filename Then
	  Local $fh = FileOpen($filename, BitOR(2,256))
	  FileWrite($fh,$str)
	  FileClose($fh)
   EndIf
   
   Return $str
EndFunc

Func save_prev_result(ByRef $last_count, $filename)
   Local $str = ""
   For $i = 0 To 14
	  $str &= "[" & $last_count[$i][0] & "," & $last_count[$i][1] & "]"
	  $str &= " 0x" & Hex($last_count[$i][2],6)
	  $str &= " " & $last_count[$i][3] & "-" & $last_count[$i][4] 
	  $str &= @CRLF
   Next
   
   If $filename Then
	  Local $fh = FileOpen($filename, BitOR(2,256))
	  FileWrite($fh,$str)
	  FileClose($fh)
   EndIf
   
   Return $str
EndFunc

Func count_last_result()
   Local $cnt[3] = [0,0,0]
   For $i = 0 To 14
	  $cnt[$last_count_result[$i][4]] += $last_count_result[$i][3]
   Next
   Return $cnt
EndFunc

Func last_result_change(ByRef $prev)
   For $i = 0 To 14
	  ;If Not ($prev[$i][3] = 0 Or $last_count_result[$i][2] = $prev[$i][2]) Then Return False
	  ;_DebugOut("compare " & Hex($last_count_result[$i][2],6) & " and " & Hex($prev[$i][2],6))
	  If $last_count_result[$i][2] <> $prev[$i][2] Then 
		 ;_DebugOut("return false")
		 Return True
	  EndIf
   Next
   Return False
EndFunc

Func scan_field($pos,$save = False)
   Local $p = $winlist[$pos]
   Local _
	  $l = $p[0] + $p[2] + $field[0], _
	  $t = $p[1] + $p[3] + $field[1], _
	  $r = $p[0] + $p[2] + $field[2], _
	  $b = $p[1] + $p[3] + $field[3], _
	  $w = $r - $l + 1, _
	  $h = $b - $t + 1, _
	  $length = $w * $h

   Local $hBMP, $tBits
   $hBMP = _ScreenCapture_Capture("",$l,$t,$r,$b)
   If $save Then
	  _ScreenCapture_SaveImage($save,$hBMP, False)
	  _DebugOut("store: " & $save)
   EndIf
   
   $tBits = DllStructCreate('dword[' & $length & ']')
   _WinAPI_GetBitmapBits($hBMP, 4 * $length, DllStructGetPtr($tBits))

   scan_field_inner($tBits,$w)
   
   _WinAPI_DeleteObject($hBMP)
   _WinAPI_DeleteObject($tBits)

   If $save Then
	  dump_last_result($save & ".txt")
   EndIf

EndFunc

Global $loaded_data = False

Func scan_field_inner(ByRef $tBits, $width)
   If Not $loaded_data Then
	  load_test($file_in)
	  $loaded_data = True
   EndIf
	  
   For $i = 0 To 14
	  Local $shift = $test_points[$i][0] + $test_points[$i][1] * $width + 1
	  Local $color = BitAND(DllStructGetData($tBits, 1, $shift),0xffffff)
	  If $color = 0 Then
		 _DebugOut($test_points[$i][0] & "," &  $test_points[$i][1] & " wrong color " & Hex(DllStructGetData($tBits, 1, $shift)))
	  EndIf
	  
	  $last_count_result[$i][0] = $test_points[$i][0] ; x
	  $last_count_result[$i][1] = $test_points[$i][1] ; y
	  $last_count_result[$i][2] = $color ; color
	  $last_count_result[$i][3] = 0 ; quantity
	  $last_count_result[$i][4] = 0 ; type: 1 - energy, 2 - cristal

	  For $j = 1 To $test_result[$i][0][0]
		 If $test_result[$i][$j][0] = $color Then
			$last_count_result[$i][3] = $test_result[$i][$j][2] ; quantity
			$last_count_result[$i][4] = $test_result[$i][$j][3] ; type: 1 - energy, 2 - cristal
			ExitLoop
		 EndIf
	  Next
   Next
EndFunc

Func load_test($filename)
   Local $fh = FileOpen($filename)
   Local $current = -1
   While True
	  Local $line = FileReadLine($fh)
	  If @error = -1 Then ExitLoop
	  Local $ret = StringRegExp($line,"\[(\d+),(\d+)\] (\d+)",1)
	  If @error = 0 Then
		 Local $i = find_in_test($ret[0],$ret[1])
		 If $i < 0 Then 
			_DebugOut("bad string '" & $line & "'")
			Exit
		 EndIf
		 $current = $i
		 $test_result[$i][0][0] = 0
	  ElseIf @error = 2 Then
		 _DebugOut("bad regexp against line '" & $line & "'")
		 Exit
	  Else
		 Local $ret = StringRegExp($line,"(0x[\da-fA-F]+) : (\d)-(\d)",1)
		 If @error = 0 Then
			Local $i = $current
			$test_result[$i][0][0] += 1
			Local $j = $test_result[$i][0][0]
			$test_result[$i][$j][0] = $ret[0]
			$test_result[$i][$j][1] = ""
			$test_result[$i][$j][2] = $ret[1]
			$test_result[$i][$j][3] = $ret[2]
		 ElseIf @error = 2 Then
			_DebugOut("bad regexp against line2 '" & $line & "'")
			Exit
		 Else
			_DebugOut("bad line '" & $line & "'")
		 EndIf
	  EndIf
   WEnd
   Fileclose($fh)
EndFunc

Func find_in_test($x,$y)
   For $i = 0 To 14
	  If $test_points[$i][0] = $x And $test_points[$i][1] = $y Then Return $i
   Next
   Return -1
EndFunc
